//
//  UserAccount.swift
//  weibopush
//
//  Created by 龚欣 on 15/6/25.
//  Copyright © 2015年 gong. All rights reserved.
//

import UIKit

class UserAccount: NSObject,NSCoding {
    /// 新浪授权的token
    var access_token : String?
    /// 过期时间
    var expires_in : NSTimeInterval = 0.0
    /// 账号的UID
    var uid : String?
    /// 和上面过期时间一样
    var remind_in : NSTimeInterval = 0.0
    /// 授权过期时间
    var expiresDate : NSDate?
    /// 账号的用户名
    var name : String?
    /// 账号的头像
    var avatar_large : String?
    
    
    init( dict : [String : NSObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
        expiresDate = NSDate(timeIntervalSinceNow: expires_in)
    }
    
    override var description : String{
        let properties = ["access_token","expires_in","remind_in","uid","expiresDate","name","avatar_large"]
        return "\(self.dictionaryWithValuesForKeys(properties))"
        
    }
    
    required init?(coder aDecoder: NSCoder) {
       access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        remind_in = aDecoder.decodeDoubleForKey("remind_in")
        uid = aDecoder.decodeObjectForKey("uid") as? String
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        name = aDecoder.decodeObjectForKey("name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
    }
    
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeDouble(remind_in, forKey: "remind_in")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
        aCoder.encodeObject(name, forKey: "name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
    }
    
    func loadUserInfo(finished : (account : UserAccount?,error : NSError?) -> ()){
    
        //发送网络请求获取用户的名称和头像 get 请求
        //https://api.weibo.com/2/users/show.json
        //access_token
        //uid
        let URLString = "2/users/show.json"

        let params = ["access_token" : access_token! , "uid" : uid!]
        print(params)
        NetworkTool.sharedNetworkTool().GET(URLString, parameters: params, success: { (_, JSON) -> Void in
            self.name = JSON["name"] as? String
            self.avatar_large = JSON["avatar_large"] as? String
            self.saveData()
            finished(account: self,error: nil)
            }) { (_, error) -> Void in
                print(error)
            finished(account: nil,error: error)
        }
        
        
    }
    
   static let path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last!.stringByAppendingPathComponent("userAccount.plist")
    
     func saveData(){
        
        print(UserAccount.path)
        NSKeyedArchiver.archiveRootObject(self, toFile: UserAccount.path)
    }
    
   class func loadData() -> UserAccount?{
    print(path)
    if let account = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? UserAccount {
        
        // 判断日期是否过期，根当前系统时间进行`比较`，低于当前系统时间，就认为过期
        // 过期日期`大于`当前日期，结果应该是降序
        if account.expiresDate!.compare(NSDate()) == NSComparisonResult.OrderedDescending {
            return account
        }
    }
    
    return nil

    }
    
}
